//
//  MainTabBarController.m
//  News
//
//  Created by lanou3g on 15/11/4.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "MainTabBarController.h"
#import "HomeViewController.h"
#import "HomeNavigationController.h"
#import "DiscoverViewController.h"
#import "UserViewController.h"
#import "ReadController.h"
#import "LoginViewController.h"
#import "MyViewController.h"


@interface MainTabBarController ()<UITabBarControllerDelegate>

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置首页
    HomeViewController *home = [[HomeViewController alloc] init];
    HomeNavigationController *homeVC = [[HomeNavigationController alloc] initWithRootViewController:home];
    homeVC.tabBarItem.title = @"新闻";
    homeVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_wenben"];
    // 取消系统 tabBarItem 自带的蓝色
    homeVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_wenben_highlight"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 给 tabBarItem 文字设置颜色
    NSMutableDictionary *homeTextDict = [NSMutableDictionary dictionary];
    homeTextDict[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [homeVC.tabBarItem setTitleTextAttributes:homeTextDict forState:UIControlStateSelected];
    
    ReadController *readVC = [[ReadController alloc] init];
    UINavigationController *readNC = [[UINavigationController alloc] initWithRootViewController:readVC];
    readVC.tabBarItem.title = @"阅读";
    readVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_yuedu"];
    // 取消系统 tabBarItem 自带的蓝色
    readVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_yuedu_highlight"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 给 tabBarItem 文字设置颜色
    NSMutableDictionary *categoryTextDict = [NSMutableDictionary dictionary];
    categoryTextDict[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [readVC.tabBarItem setTitleTextAttributes:categoryTextDict forState:UIControlStateSelected];
    
    
    
    DiscoverViewController *discoverVC = [[DiscoverViewController alloc] init];
    UINavigationController *discoverNC = [[UINavigationController alloc] initWithRootViewController:discoverVC];
    discoverVC.tabBarItem.title = @"发现";
    discoverVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_iconfaxian"];
    // 取消系统 tabBarItem 自带的蓝色
    discoverVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_iconfaxian_highlight"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 给 tabBarItem 文字设置颜色
    NSMutableDictionary *discoverTextDict = [NSMutableDictionary dictionary];
    discoverTextDict[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [discoverVC.tabBarItem setTitleTextAttributes:discoverTextDict forState:UIControlStateSelected];
    
    
    // 设置用户
    MyViewController *userVC = [[MyViewController alloc] init];
    UINavigationController *userNC = [[UINavigationController alloc] initWithRootViewController:userVC];
    userVC.tabBarItem.title = @"用户";
    userVC.tabBarItem.image = [UIImage imageNamed:@"tabbar_yonghu"];
    // 取消系统 tabBarItem 自带的蓝色
    userVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_yonghu_highlight"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 给 tabBarItem 文字设置颜色
    NSMutableDictionary *userTextDict = [NSMutableDictionary dictionary];
    userTextDict[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [userVC.tabBarItem setTitleTextAttributes:userTextDict forState:UIControlStateSelected];
    
    self.viewControllers = @[homeVC, readNC, discoverNC, userNC];
    self.delegate = self;
    
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    UINavigationController * fourNav = (UINavigationController *)viewController;
    if ([fourNav.viewControllers.firstObject isKindOfClass:[MyViewController class]]) {
        if (![LoginViewController isLogin]) {
            self.selectedIndex = 0;
            UserViewController *loginVC = [[UserViewController alloc] init];
            UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:loginVC];
            [self presentViewController:nc animated:YES completion:nil];
        }
    }
}

@end
